<?php

//<<<<<<< .mine
//<?php

require_once './_customer.inc';
require_once './_common.inc';
require_once './_rsa.inc';
require_once './config.inc';
require_once './_bank.inc'; // for joining & bank signing testing



$n = 100; // n for number of ID strings

// Secret splitting.  ID string must be less than 48 characters
// 
// Secret is a 2D array. The first array is the value of 'n' for money orders
// For the second array, 0 is "left" and 1 is "right"
$id_string = 'Alice:Smith:12345678900000893';
$secret = secret_split_array($id_string, $n);
	
// Dummy stuff for MO object

$mo['uniquestring'] = generate_us();
$mo['amount'] = 1000;

// $commit is a 2D array of md5 hashes of $secret's values
$commit = commit_id_strings($secret);

$mo['id_hash'] = $commit;

echo "<PRE>";

echo $secret[6][0] . "\n";
echo $secret[6][1] . "\n\n";

$s = join_id_string($secret[6][0], $secret[6][1]);
echo "Partial rejoining just to be sure it worked:    " . $s . "\n\n";


echo "First hash:  " . $commit[0][0] . "\n\n"; 

$r = generate_r(MODULUS);

echo "R: " . $r . "<BR>";

echo "GCD(R,N) = " . gmp_strval(gmp_gcd($r,MODULUS)) . "<BR><BR>";


// r^-1 is also calculated later for the unblinding function, which only needs 'r'
$ri = gmp_strval(gmp_invert($r,MODULUS));

echo "R^-1 (also calculated later): " . $ri . "<BR><BR>";

echo "R * R^-1 % N: " . bcmod(bcmul($r,$ri),MODULUS) . "<BR><BR>";


// Blinded MOs, sent to bank.
$mo_string = mo_obj_to_string($mo);

echo "Money order string: " . $mo_string . "\n\n";

$h = md5($mo_string);

echo "MD5 hash of Money order string:  " . $h . "<BR><BR>";

$blind = blind($h, PUBLIC_EXP, MODULUS, $r);

echo "Blinded money order:  " . $blind . "<BR><BR>";

$banksign = blind_sign2($blind); // done by bank

echo "Blind signature returned from bank:  " . $banksign . "<BR><BR>";
echo "Hex of Blind signature returned from bank:  " . dec_to_hex($banksign) . "<BR><BR>";

$signedMO = unblind($banksign,$r,MODULUS);

echo "Signed M: " . $signedMO . "<BR>";

// Checking to see if it worked--

$h2 = verify_signature($signedMO, PUBLIC_EXP, MODULUS);

echo "Hash of MO string? : " . $h2 . "<BR><BR>";

if(is_valid_signature($mo_string, $signedMO))
	echo "The signature is valid.\n\n";
else
	echo "The signature is NOT valid.\n\n";

echo "</PRE>";


/*
?>
>>>>>>> .r112

$map = array( 'version'    => 4,
              'OS'         => 'Linux',
              'lang'       => 'english',
              'short_tags' => true
            );
$map1 = array( 'version'    => 4,
              'OS'         => 'Linux',
              'lang'       => 'english',
              'short_tags' => true
            );
            if (array_diff($map, $map1)) echo "true";
            echo $map[version];
          $fruits = array ( "fruits"  => array ( "a" => "orange",
                                       "b" => "banana",
                                       "c" => "apple"
                                     ),
                  "numbers" => array ( 1,
                                       2,
                                       3,
                                       4,
                                       5,
                                       6
                                     ),
                  "holes"   => array (      "first",
                                       5 => "second",
                                            "third"
                                     )
                );
                echo sizeof($fruits);
          echo <<<END
			<input type='hidden' name='chosen' value="$map[version]">;
          
END;
*/?>